<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:q="livcos.org/ns/query"
	xmlns:db="livcos.org/ns/db"
	xmlns:ddl="livcos.org/ns/db/ddl"
	xmlns:vz="livcos.org/ns/doc/visualization"
	xmlns:idx="livcos.org/ns/index"
	xmlns="http://www.w3.org/1999/xhtml"
	xmlns:f="livcos.org/ns/xslt/function"
	xmlns:v="livcos.org/ns/view"
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
	exclude-result-prefixes="q db ddl vz idx f v xs">
	
	<xsl:import href="/livcos.org/type/livcos.org/ns/query/data.xsl"/>
	<xsl:import href="../doc/visualization/view.xsl"/>
	
	<xsl:template match="q:xpath">
		<xsl:variable name="data" as="element()">
			<xsl:apply-templates select="." mode="data"/>
		</xsl:variable>
		<xsl:variable name="viz">
			<table id="{@id}" xmlns="livcos.org/ns/doc/visualization">
				<column label="Object" select="@obj"/>
				<column label="Node ID" select="@node"/>
				<column label="Type" select="node-name(node())"/>
				<column label="Value" select="."/>
				<data content-type="text/xml">
					<xsl:sequence select="$data"/>
				</data>
			</table>
		</xsl:variable>
		<xsl:apply-templates select="$viz/*"/>
	</xsl:template>
	
	<xsl:template match="q:search">
		<xsl:variable name="data" as="element()">
			<xsl:apply-templates select="." mode="data"/>
		</xsl:variable>
		<xsl:variable name="viz">
			<xsl:variable name="resultsId" select="'/livcos.org/web/doc/ns/livcos.org/ns/query/search'"/>
			<xsl:variable name="defaultViz" select="f:entity(@index)/idx:query/idx:results[@ref = $resultsId]/vz:*"/>
			<xsl:choose>
				<xsl:when test="$defaultViz">
					<xsl:element name="{local-name($defaultViz)}" namespace="livcos.org/ns/doc/visualization">
						<xsl:attribute name="id" select="@id"/>
						<xsl:copy-of select="$defaultViz/*"/>
						<data content-type="text/xml" xmlns="livcos.org/ns/doc/visualization">
							<xsl:sequence select="$data"/>
						</data>
					</xsl:element>
				</xsl:when>
				<xsl:when test="$data/*/*[1]/title">
					<table id="{@id}" xmlns="livcos.org/ns/doc/visualization">
						<column label="Title" select="title" link="obj"/>
						<column label="Description" select="summary"/>
						<data content-type="text/xml">
							<xsl:sequence select="$data"/>
						</data>
					</table>
				</xsl:when>
				<xsl:when test="not($data/*/*)">
					<table id="{@id}" xmlns="livcos.org/ns/doc/visualization">
						<column label="-- Text Search --" select="title"/>
						<data content-type="text/xml">
							<tbl>
								<row><col>-- no results --</col></row>
							</tbl>
						</data>
					</table>
				</xsl:when>
				<xsl:otherwise>
					<table id="{@id}" xmlns="livcos.org/ns/doc/visualization">
						<data content-type="text/xml">
							<xsl:sequence select="$data"/>
						</data>
					</table>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:apply-templates select="$viz/*"/>
	</xsl:template>
	
	<xsl:template match="q:sql">
		<xsl:variable name="data" as="element()">
			<xsl:apply-templates select="." mode="data"/>
		</xsl:variable>
		<xsl:variable name="viz">
			<table id="{@id}" xmlns="livcos.org/ns/doc/visualization">
				<xsl:choose>
					<xsl:when test="q:query/@groups">
						<xsl:apply-templates select="f:parse-groups(q:query/@groups)/*" mode="query_viz">
							<xsl:with-param name="cols" select="$data/*/ddl:meta/ddl:column"/>
						</xsl:apply-templates>
					</xsl:when>
					<xsl:otherwise>
						<rows>
							<xsl:apply-templates select="$data/*/ddl:meta/ddl:column" mode="query_viz"/>
						</rows>
					</xsl:otherwise>
				</xsl:choose>
				<data content-type="text/xml">
					<xsl:element name="{name($data)}" namespace="">
						<xsl:copy-of select="$data/@*"/>
						<xsl:element name="{name($data/*)}" namespace="livcos.org/ns/db">
							<xsl:copy-of select="$data/*/@*"/>
							<xsl:copy-of select="$data/*/db:*"/>
						</xsl:element>
					</xsl:element>
				</data>
			</table>
		</xsl:variable>
		<xsl:apply-templates select="$viz/*"/>
	</xsl:template>
	
	<xsl:template match="g" mode="query_viz">
		<xsl:param name="cols"/>
		<xsl:param name="start" select="1"/>
		<rows xmlns="livcos.org/ns/doc/visualization">
			<xsl:variable name="end" select="$start + @count"/>
			<xsl:apply-templates select="$cols[(position() ge $start) and (position() lt $end)]" mode="#current"/>
			<xsl:apply-templates select="g" mode="#current">
				<xsl:with-param name="cols" select="$cols"/>
				<xsl:with-param name="start" select="$end"/>
			</xsl:apply-templates>
		</rows>
	</xsl:template>
	
	<xsl:template match="ddl:column" mode="query_viz">
		<xsl:variable name="t">
			<xsl:for-each select="@catalog | @schema | @table">
				<xsl:value-of select="."/>
				<xsl:text>.</xsl:text>
			</xsl:for-each>
			<xsl:value-of select="@name"/>
			<xsl:if test="not(@name)">
				<xsl:value-of select="@label"/>
			</xsl:if>
			<xsl:text> [</xsl:text>
			<xsl:value-of select="@type"/>
			<xsl:if test="@size">
				<xsl:text>(</xsl:text>
				<xsl:value-of select="@size"/>
				<xsl:if test="@scale">
					<xsl:text>,</xsl:text>
					<xsl:value-of select="@scale"/>
				</xsl:if>
				<xsl:text>)</xsl:text>
				<xsl:if test="@nullable">
					<xsl:text>, </xsl:text>
					<xsl:value-of select="if (xs:boolean(@nullable)) then 'null' else 'not null'"/>
				</xsl:if>
			</xsl:if>
			<xsl:text>]</xsl:text>
		</xsl:variable>
		<column label="{@label}" title="{$t}" xmlns="livcos.org/ns/doc/visualization">
			<xsl:if test="@type and index-of(('int', 'smallint'), @type)">
				<xsl:attribute name="align">right</xsl:attribute>
			</xsl:if>
		</column>
	</xsl:template>
</xsl:stylesheet>
